﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.IO;
using System.Data.SqlClient;


namespace lcps.Web.Admin
{
    public partial class search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ty"] != null)
                {
                    string ty = Request.QueryString["ty"].ToString();
                    if (ty == "4")
                        btn_out4.CssClass = "mybtn";
                    if (ty == "1")
                        btn_out1.CssClass = "mybtn";
                    if (ty == "2")
                        btn_out2.CssClass = "mybtn";
                    if (ty == "3")
                        btn_out3.CssClass = "mybtn";
                }
                //增加数据 日期的处理
                lcps.BLL.tableinfo bt = new BLL.tableinfo();
                ddltables.DataSource = bt.GetAllList().Tables[0];
                ddltables.DataTextField = "tablename";
                ddltables.DataValueField = "id";

                ddltables.DataBind();
                ddltables.Items.Insert(0, "所有");
                ddlcol.Items.Clear();
                ddlcol.Items.Add(new ListItem("项目编号", "proid"));
                ddlcol.Items.Add(new ListItem("招标计划编号", "bidid"));
                ddlcol.Items.Add(new ListItem("采购订单号", "purchaseid"));
                ddlcol.Items.Add(new ListItem("需用单位", "usework"));
                ddlcol.Items.Add(new ListItem("项目名称", "projectname"));
                ddlcol.Items.Add(new ListItem("项目类型", "projecttype"));
                ddlcol.Items.Add(new ListItem("供应商名称", "supplier"));
                ddlcol.Items.Add(new ListItem("物料描述", "materiel"));
                ddlcol.Items.Add(new ListItem("单位", "unit"));
                ddlcol.Items.Add(new ListItem("数量", "nums"));
                ddlcol.Items.Add(new ListItem("合同签定日期", "contractdate"));
                ddlcol.Items.Add(new ListItem("合同交货时间", "deliverydate"));
                ddlcol.Items.Add(new ListItem("确定交货期", "confirmdate"));
                ddlcol.Items.Add(new ListItem("实际到货时间", "realdate"));
                ddlcol.Items.Add(new ListItem("办理出入库时间", "realdate"));
                ddlcol.Items.Add(new ListItem("项目单位要求到货时间", "arrivaldate"));
                ddlcol.Items.Add(new ListItem("需要协调的问题", "coordinate"));
                ddlcol.Items.Add(new ListItem("备注", "remark"));
                ddlcol.Items.Add(new ListItem("接收人", "receivedname"));
                ddlcol.Items.Add(new ListItem("接收人联系电话", "receivedphone"));
                ddlcol.Items.Add(new ListItem("供应商联系人", "suppliername"));
                ddlcol.Items.Add(new ListItem("供应商联系电话", "supplierphone"));
                ddlcol.Items.Add(new ListItem("验收单办理", "checkstate"));
                ddlcol.Items.Add(new ListItem("办理日期", "transactdate"));
                ddlcol.Items.Add(new ListItem("经办人", "agent"));
                ddlcol.Items.Add(new ListItem("项目计划投运时间", "workdate"));
                ddlcol.Items.Add(new ListItem("图纸设计计划完成时间", "drawingplandate"));
                ddlcol.Items.Add(new ListItem("目前图纸设计情况", "drawingstate"));
                ddlcol.Items.Add(new ListItem("图纸应提交设计单位时间", "drawingsubmitdate"));
                ddlcol.Items.Add(new ListItem("要求设计单位确认时间", "drawingconfirmdate"));
                ddlcol.Items.Add(new ListItem("设计单位确认情况", "designstate"));
                ddlcol.Items.Add(new ListItem("主要原材料采购情况", "majormaterielpurchase"));
                ddlcol.Items.Add(new ListItem("目前设备生产状态", "deviceproductionstate"));
                ddlcol.Items.Add(new ListItem("本月生产情况", "mothstate"));
                ddlcol.Items.Add(new ListItem("下月生产计划", "nextmoth"));
                ddlcol.Items.Add(new ListItem("计划到货时间", "planarrivedate"));

            }
            this.lblsql.Text = hfsql.Value;

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            mybind();
        }

        private void mybind()
        {
            lcps.BLL.projectinfo bp = new BLL.projectinfo();
            string sql = hfsql.Value;
            DataSet ds = null;
            if (sql == "")
            {
                if (ddltables.SelectedValue != "所有")
                    ds = bp.GetList(" tableid=" + ddltables.SelectedValue);
                else
                    ds = bp.GetList("");
            }
            else
            {
                if (ddltables.SelectedValue != "所有")
                    ds = bp.GetList(sql + " and tableid=" + ddltables.SelectedValue);
                else
                    ds = bp.GetList(sql);
            }
            gwSearch.DataSource = ds.Tables[0];
            gwSearch.DataBind();
            gwSearch.Width = 3500;
            this.lblcount.Text = "共" + ds.Tables[0].Rows.Count.ToString() + "纪录";            
        }

        protected void btn_out1_Click(object sender, EventArgs e)
        {
            this.gwSearch.AllowPaging = false;

            string oldHfsql = hfsql.Value;//记录旧的查询条件

            if (!string.IsNullOrEmpty(hfsql.Value))
            {
                hfsql.Value += " and   realdate is null "; 
            }
            else
            {
                hfsql.Value += " 1=1 and   realdate is null "; 
            }
           

            mybind();

            hfsql.Value = oldHfsql;

            //this.gwSearch.Columns[0].Visible = false;
            //this.gwSearch.Columns[1].Visible = false;
            //this.gwSearch.Columns[3].Visible = false;
            //this.gwSearch.Columns[6].Visible = false;
            //this.gwSearch.Columns[8].Visible = false;
            //this.gwSearch.Columns[17].Visible = false;
            //this.gwSearch.Columns[18].Visible = false;
            //this.gwSearch.Columns[19].Visible = false;
            //this.gwSearch.Columns[20].Visible = false;
            //this.gwSearch.Columns[22].Visible = false;
            //this.gwSearch.Columns[23].Visible = false;
            //this.gwSearch.Columns[24].Visible = false;
            //this.gwSearch.Columns[25].Visible = false;
            //this.gwSearch.Columns[26].Visible = false;
            //this.gwSearch.Columns[27].Visible = false;
            //this.gwSearch.Columns[28].Visible = false;
            //this.gwSearch.Columns[29].Visible = false;
            //this.gwSearch.Columns[30].Visible = false;
            //this.gwSearch.Columns[31].Visible = false;
            //this.gwSearch.Columns[32].Visible = false;
            //this.gwSearch.Columns[33].Visible = false;
            //this.gwSearch.Columns[35].Visible = false;
            //this.gwSearch.Columns[36].Visible = false;

            if (this.gwSearch.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.Year + "wdhmxb.xls");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>" + DateTime.Now.Year + "年   项目物资供应计划未到货明细表</H4>");
                //Response.Output.Write("<H5 align='left'>共有条" + this.gwSearch.Rows.Count + "记录</H5>");
                this.gwSearch.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }

        protected void btn_out2_Click(object sender, EventArgs e)
        {
            this.gwSearch.AllowPaging = false;

            mybind();

            this.gwSearch.Columns[0].Visible = false;
            this.gwSearch.Columns[1].Visible = false;
            this.gwSearch.Columns[3].Visible = false;
            this.gwSearch.Columns[6].Visible = false;
            this.gwSearch.Columns[8].Visible = false;
            this.gwSearch.Columns[14].Visible = false;
            this.gwSearch.Columns[16].Visible = false;
            this.gwSearch.Columns[17].Visible = false;
            this.gwSearch.Columns[18].Visible = false;
            this.gwSearch.Columns[19].Visible = false;
            this.gwSearch.Columns[20].Visible = false;
            this.gwSearch.Columns[22].Visible = false;
            this.gwSearch.Columns[23].Visible = false;
            this.gwSearch.Columns[24].Visible = false;
            this.gwSearch.Columns[25].Visible = false;
            this.gwSearch.Columns[26].Visible = false;
            this.gwSearch.Columns[27].Visible = false;
            this.gwSearch.Columns[28].Visible = false;
            this.gwSearch.Columns[29].Visible = false;
            this.gwSearch.Columns[30].Visible = false;
            this.gwSearch.Columns[31].Visible = false;
            this.gwSearch.Columns[32].Visible = false;
            this.gwSearch.Columns[33].Visible = false;
            this.gwSearch.Columns[35].Visible = false;
            this.gwSearch.Columns[36].Visible = false;

            if (this.gwSearch.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.Year + "xmwzgyjhb.xls");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>" + DateTime.Now.Year + "年   项目物资供应计划表</H4>");
                //Response.Output.Write("<H5 align='left'>共有条" + this.gwSearch.Rows.Count + "记录</H5>");
                this.gwSearch.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }

        protected void btn_out3_Click(object sender, EventArgs e)
        {
            this.gwSearch.AllowPaging = false;

            string oldHfsql = hfsql.Value;//记录旧的查询条件

            if (!string.IsNullOrEmpty(hfsql.Value))
            {
                hfsql.Value += " and dealStorageDate is not null and  realdate is null "; 
            }
            else
            {
                hfsql.Value += " 1=1 and  dealStorageDate is not null and  realdate is null "; 
            }

            mybind();

            hfsql.Value = oldHfsql;
            

            if (this.gwSearch.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.Year + "xmwzxqjscqkyb.xls");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>聊城供电公司   项目物资需求及生产情况月报</H4>");
                //Response.Output.Write("<H5 align='left'>共有条" + this.gwSearch.Rows.Count + "记录</H5>");
                this.gwSearch.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }

        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }

        protected void btn_out4_Click(object sender, EventArgs e)
        {

            this.gwSearch.AllowPaging = false;

            mybind();

            this.gwSearch.Columns[0].Visible = false;
            this.gwSearch.Columns[1].Visible = false;

            for (int i = 0; i < this.gwSearch.Rows.Count; i++)
            {
                CheckBox cb = this.gwSearch.Rows[i].FindControl("chku") as CheckBox;

                cb.Visible = false;
            }

            if (this.gwSearch.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.Year + "zb.xls");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>聊城供电公司   项目物资需求计划履约明细表（总表）</H4>");
                //Response.Output.Write("<H5 align='left'>共有条" + this.gwSearch.Rows.Count + "记录</H5>");
                this.gwSearch.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }

        protected void gwSearch_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gwSearch.EditIndex = e.NewEditIndex;
            //DropDownList drpDwnLst = (DropDownList)this.gwSearch.Rows[e.NewEditIndex].FindControl("ddlsupplier");
            //string name = DataBinder.Eval(this.gwSearch.Rows[e.NewEditIndex].DataItem, "supplier").ToString();



            mybind();

        }

        protected void gwSearch_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            lcps.Model.projectinfo model = new lcps.Model.projectinfo();
            model.ID = int.Parse(gwSearch.DataKeys[e.RowIndex].Value.ToString());
            model.proid = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[3].Controls[1]).Text;
            model.bidid = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            model.purchaseid = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
            model.usework = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
            model.projectname = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
            model.projecttype = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[8].Controls[0]).Text;
            DropDownList ddlsupplier = ((DropDownList)gwSearch.Rows[e.RowIndex].FindControl("ddlsupplier"));
            model.supplier = ddlsupplier.SelectedItem.Text;
            int id = int.Parse(ddlsupplier.SelectedValue.ToString());
            lcps.BLL.supplierinfo sll = new BLL.supplierinfo();
            lcps.Model.supplierinfo supply = sll.GetModel(id);
            model.suppliername = supply.suppliername;
            model.supplierphone = supply.supplierphone;
            model.materiel = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
            string nums = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[11].Controls[0]).Text;
            if (nums.Trim() != "")
                model.nums = float.Parse(nums);
            model.unit = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[12].Controls[0]).Text;
            string mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtcontractdate")).Text;
            if (mydate.Trim() != "")
                model.contractdate = DateTime.Parse(mydate);
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtdeliverydate")).Text;
            if (mydate.Trim() != "")
                model.deliverydate = DateTime.Parse(mydate);
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtconfirmdate")).Text;
            if (mydate.Trim() != "")
                model.confirmdate = DateTime.Parse(mydate);
            //实际到货日期  2013-10-16
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtrealdate")).Text;
            if (mydate.Trim() != "")
                model.realdate = DateTime.Parse(mydate);
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtarrivaldate")).Text;
            if (mydate.Trim() != "")
                model.arrivaldate = DateTime.Parse(mydate);
          
            //办理出入库时间
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtdealStorageDate")).Text;
            if (mydate.Trim() != "")
                model.dealStorageDate = DateTime.Parse(mydate);

            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtworkdate")).Text;
            if (mydate.Trim() != "")
                model.workdate = DateTime.Parse(mydate);
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtdrawingplandate")).Text;
            if (mydate.Trim() != "")
                model.drawingplandate = DateTime.Parse(mydate);
            //目前图纸设计情况
            model.drawingstate = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[29].Controls[0]).Text;
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtdrawingsubmitdate")).Text;
            if (mydate.Trim() != "")
                model.drawingsubmitdate = DateTime.Parse(mydate);
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtdrawingconfirmdate")).Text;
            if (mydate.Trim() != "")
                model.drawingconfirmdate = DateTime.Parse(mydate);
            model.designstate = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[32].Controls[0]).Text;
            model.majormaterielpurchase = ((DropDownList)gwSearch.Rows[e.RowIndex].FindControl("ddlmajormaterielpurchase")).Text;
            model.deviceproductionstate = ((DropDownList)gwSearch.Rows[e.RowIndex].FindControl("txtdeviceproductionstate")).Text;
            model.mothstate = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[35].Controls[0]).Text;
            model.nextmoth = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[36].Controls[0]).Text;
            mydate = ((TextBox)gwSearch.Rows[e.RowIndex].FindControl("txtplanarrivedate")).Text;
            if (mydate.Trim() != "")
                model.planarrivedate = mydate;
            model.coordinate = ((DropDownList)gwSearch.Rows[e.RowIndex].FindControl("ddlcoordinate")).Text;
            model.remark = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[22].Controls[0]).Text;
           
            model.receivedname = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[23].Controls[0]).Text;
            model.receivedphone = ((TextBox)gwSearch.Rows[e.RowIndex].Cells[24].Controls[0]).Text;
            lcps.BLL.projectinfo bll = new lcps.BLL.projectinfo();
            bool bl = bll.UpdateGV(model);

            gwSearch.EditIndex = -1;
            mybind();

        }

        protected void gwSearch_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(gwSearch.DataKeys[e.RowIndex].Value.ToString());
            lcps.BLL.projectinfo bll = new BLL.projectinfo();
            bll.Delete(id);
            mybind();

        }

        protected void gwSearch_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 用户权限设置
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    string str = ds.Tables[0].Rows[0]["userrank"].ToString();//获取用户权限
                    LinkButton hy_edit = e.Row.FindControl("hy_edit") as LinkButton;//获取编辑按钮

                    if (!string.IsNullOrEmpty(str))
                    {
                        string[] tableIds = str.Split('|');

                        Label lbl_tableid = e.Row.FindControl("lbl_tableid") as Label;//获取项目类型

                        bool flag = false;

                        foreach (string tableid in tableIds)
                        {
                            if (tableid == lbl_tableid.Text)
                            {
                                flag = true;
                                //hy_edit.Enabled = true;
                            }
                        }

                        if (hy_edit != null)
                        {
                            hy_edit.Enabled = flag;
                        }


                    }
                    else
                    {
                        if (hy_edit != null)
                        {
                            hy_edit.Enabled = true;
                        }

                    }

                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this, "top.location.href='../Default.aspx'");
                }
                #endregion

                #region 设置预警
                //string 确定交货期预警 = "lblconfirmDateWarn";
                //string 结算时间预警 = "lblaccountsDateWarn  ";

                Label lblisWarn = e.Row.FindControl("lblisWarn") as Label;//是否预警

                Label lblconfirmdate = e.Row.FindControl("lblconfirmdate") as Label;//确定交货期
                Label lblaccountdate = e.Row.FindControl("lblrealdate") as Label;//实际到货时间
                Label lblcontractdate = e.Row.FindControl("lblcontractdate") as Label;//合同交货时间
                Label lbldealStorageDate = e.Row.FindControl("lbldealStorageDate") as Label;//办理出入库时间

                if (lblisWarn != null)
                {
                    if (lblisWarn.Text == "0")
                    {
                        #region 预警操作
                        //确定交货期预警
                        if (lblconfirmdate != null && lblcontractdate != null)
                        {
                            if (!string.IsNullOrEmpty(lblconfirmdate.Text.Trim()))
                            {
                                DateTime confirmdateDT = DateTime.Parse(lblconfirmdate.Text.Trim());//确定交货期时间
                                TimeSpan ts = confirmdateDT - DateTime.Now;
                                int confirm_day = ts.Days;
                                if (confirm_day <= 10)
                                {
                                    e.Row.Cells[17].BackColor = System.Drawing.Color.DeepPink;
                                    Label lblwarn = e.Row.FindControl("lblconfirmDateWarn") as Label;
                                    lblwarn.Text = confirm_day.ToString();
                                }
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(lblcontractdate.Text.Trim()))
                                {
                                    DateTime confirmdateDT = DateTime.Parse(lblcontractdate.Text.Trim());//合同交货时间
                                    TimeSpan ts = confirmdateDT - DateTime.Now;
                                    int confirm_day = ts.Days;
                                    if (confirm_day <= 10)
                                    {
                                        e.Row.Cells[17].BackColor = System.Drawing.Color.DeepPink;
                                        Label lblwarn = e.Row.FindControl("lblconfirmDateWarn") as Label;
                                        lblwarn.Text = confirm_day.ToString();
                                    }
                                }

                            }
                        }


                        //结算时间预警
                        if (lblaccountdate != null && lblcontractdate != null)
                        {
                            if (!string.IsNullOrEmpty(lblaccountdate.Text.Trim()))
                            {
                                DateTime confirmdateDT = DateTime.Parse(lblaccountdate.Text.Trim());//实际到货时间
                                TimeSpan ts = confirmdateDT - DateTime.Now;
                                int confirm_day = ts.Days;
                                if (confirm_day <= 25)
                                {
                                    e.Row.Cells[18].BackColor = System.Drawing.Color.DeepPink;
                                    Label lblwarn = e.Row.FindControl("lblaccountsDateWarn") as Label;
                                    lblwarn.Text = confirm_day.ToString();
                                }
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(lblcontractdate.Text.Trim()))
                                {
                                    DateTime confirmdateDT = DateTime.Parse(lblcontractdate.Text.Trim());//合同交货时间
                                    TimeSpan ts = confirmdateDT - DateTime.Now;
                                    int confirm_day = ts.Days;
                                    if (confirm_day <= 25)
                                    {
                                        e.Row.Cells[18].BackColor = System.Drawing.Color.DeepPink;
                                        Label lblwarn = e.Row.FindControl("lblaccountsDateWarn") as Label;
                                        lblwarn.Text = confirm_day.ToString();
                                    }
                                }
                            }
                        }
                        #endregion
                    }

                    //if (lbldealStorageDate != null)//如果办理出入库时间有，则取消预警
                    //{
                    //    if (!string.IsNullOrEmpty(lbldealStorageDate.Text))
                    //    {
                            
                    //    }
                    //}
                }
                
               
              

                #endregion
            }
            Label lblid = e.Row.FindControl("lblNum") as Label;
            //Label6
            Label lbltime = e.Row.FindControl("lblconfirmdate") as Label;
            //Label1
            Label lblsta = e.Row.FindControl("Label1") as Label;//目前设备生产状态
            //DateTime dt;
            //if (lblsta != null && lblsta.Text != "已到货")
            //    if (lbltime != null && lbltime.Text != "")
            //    {
            //        dt = DateTime.Parse(lbltime.Text);
            //        if (dt.AddDays(-7) <= DateTime.Now)
            //        {
            //            e.Row.BackColor = System.Drawing.Color.Red;
            //        }

            //    }
            if (lblid != null) lblid.Text = (e.Row.RowIndex + 1).ToString();
            if ((DropDownList)e.Row.FindControl("ddlsupplier") != null)
            {
                DropDownList ddlCollege = (DropDownList)e.Row.FindControl("ddlsupplier");
                lcps.BLL.supplierinfo bll = new BLL.supplierinfo();
                DataSet ds = bll.GetListDDL(" 1=1 order by supplyname");
                ddlCollege.DataSource = ds;
                ddlCollege.DataTextField = "supplyname";
                ddlCollege.DataValueField = "ID";
                ddlCollege.DataBind();
                string name = DataBinder.Eval(e.Row.DataItem, "supplier").ToString();
                ddlCollege.SelectedIndex = ddlCollege.Items.IndexOf(ddlCollege.Items.FindByText(name));
                DropDownList ddlmajor = (DropDownList)e.Row.FindControl("ddlmajormaterielpurchase");
                string major = DataBinder.Eval(e.Row.DataItem, "majormaterielpurchase").ToString();
                ddlmajor.SelectedIndex = ddlmajor.Items.IndexOf(ddlmajor.Items.FindByText(major));

                DropDownList ddldevice = (DropDownList)e.Row.FindControl("txtdeviceproductionstate");
                string device = DataBinder.Eval(e.Row.DataItem, "deviceproductionstate").ToString();
                ddldevice.SelectedIndex = ddldevice.Items.IndexOf(ddldevice.Items.FindByText(device));

                DropDownList ddlcoordinate = (DropDownList)e.Row.FindControl("ddlcoordinate");
                string coordinate = DataBinder.Eval(e.Row.DataItem, "coordinate").ToString();
                ddlcoordinate.SelectedIndex = ddlcoordinate.Items.IndexOf(ddlcoordinate.Items.FindByText(coordinate));




            }

        }

        protected void gwSearch_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gwSearch.EditIndex = -1;
            mybind();

        }
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gwSearch.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gwSearch.Rows[i].FindControl("chku");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gwSearch.DataKeys[i].Value != null)
                    {
                        idlist += gwSearch.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }
        protected void btndel_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            lcps.BLL.projectinfo p = new BLL.projectinfo();
            p.DeleteList(idlist);
            mybind();
        }

        protected void gwSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gwSearch.PageIndex = e.NewPageIndex;
            mybind();
        }
        //删除选定项目
        protected void btndeltableid_Click(object sender, EventArgs e)
        {
            string value = this.ddltables.SelectedItem.Value;

            if (value == "所有")
            {
                Maticsoft.Common.MessageBox.Show(this, "请选择要删除的项目");
            }
            else
            {
                lcps.BLL.projectinfo p = new BLL.projectinfo();

                if (p.DeleteByWhere(" where tableid=" + value))
                {
                    Maticsoft.Common.MessageBox.Show(this, "删除成功！");
                    mybind();
                }
            }
        }

        protected void btnsetstatus_Click(object sender, EventArgs e)
        {
            string idlist;
            if (chbzt.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = ddlstatus_subUp.SelectedItem.Text;
                lcps.BLL.projectinfo p = new BLL.projectinfo();
                p.setstatusList(idlist, sta);

            }
            if (chbbz.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = txtbz_subUp.Text;
                lcps.BLL.projectinfo p = new BLL.projectinfo();
                p.setbzList(idlist, sta);
            }
            if (chbxt.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = ddlcoordinate_subUp.SelectedItem.Text;
                lcps.BLL.projectinfo p = new BLL.projectinfo();
                p.setcoordinateList(idlist, sta);
            }
            if (chbjsr.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = txtreceivedname_subUp.Text;
                lcps.BLL.projectinfo p = new BLL.projectinfo();
                p.setreceivednameList(idlist, sta);
            }
            if (chbsj.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = txtrellytime_subUp.Text;
                if (!string.IsNullOrEmpty(sta))
                {
                    lcps.BLL.projectinfo p = new BLL.projectinfo();
                    p.setrealtimeList(idlist, sta);
                }
                
            }
            if (chbjh.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = txtplanarrivedate_subUp.Text;
                if (!string.IsNullOrEmpty(sta))
                {
                    lcps.BLL.projectinfo p = new BLL.projectinfo();
                    p.setplanarrivedateList(idlist, sta);
                }
               
            }
            //确定交货期
            if (chbqdjhq.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = txtconfirmdate_subUp.Text;
                if (!string.IsNullOrEmpty(sta))
                {
                    lcps.BLL.projectinfo p = new BLL.projectinfo();
                    p.setconfirmdate(idlist, sta);
                }
               
            }
            //办理出入库时间
            if (chbblcrksj.Checked)
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;
                string sta = txtdealStorage_subUp.Text;
                if (!string.IsNullOrEmpty(sta))
                {
                    lcps.BLL.projectinfo p = new BLL.projectinfo();
                    p.setdealStorageDate(idlist, sta);

                    p.setIsWarn(idlist, "1");//如果有办理出入库时间，则取消预警
                    
                }
             
            }
            mybind();

        }


        protected void btnNo_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gwSearch.Columns.Count; i++)
                gwSearch.Columns[i].Visible = true;
            this.gwSearch.Columns[3].Visible = false;
            this.gwSearch.Columns[6].Visible = false;
            this.gwSearch.Columns[8].Visible = false;
            this.gwSearch.Columns[15].Visible = false;
            this.gwSearch.Columns[16].Visible = false;
            this.gwSearch.Columns[17].Visible = false;
            this.gwSearch.Columns[18].Visible = false;
            this.gwSearch.Columns[19].Visible = false;
            this.gwSearch.Columns[20].Visible = false;
            this.gwSearch.Columns[21].Visible = false;
            this.gwSearch.Columns[22].Visible = false;
            this.gwSearch.Columns[24].Visible = false;
            this.gwSearch.Columns[25].Visible = false;
            this.gwSearch.Columns[28].Visible = false;
            this.gwSearch.Columns[30].Visible = false;
            this.gwSearch.Columns[31].Visible = false;
            this.gwSearch.Columns[32].Visible = false;
            this.gwSearch.Columns[33].Visible = false;
            this.gwSearch.Columns[34].Visible = false;

            this.gwSearch.Width = 1500;
            mybind();
        }
        protected void btnNo1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gwSearch.Columns.Count; i++)
                gwSearch.Columns[i].Visible = true;
            this.gwSearch.Columns[3].Visible = false;
            this.gwSearch.Columns[13].Visible = false;
            this.gwSearch.Columns[29].Visible = false;
            this.gwSearch.Columns[30].Visible = false;
            this.gwSearch.Columns[31].Visible = false;
            this.gwSearch.Columns[32].Visible = false;
            this.gwSearch.Columns[33].Visible = false;
            this.gwSearch.Columns[34].Visible = false;
            mybind();
        }
        protected void btnNoa_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gwSearch.Columns.Count; i++)
                gwSearch.Columns[i].Visible = true;
            mybind();
        }
        protected void btnNo0_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gwSearch.Columns.Count; i++)
                gwSearch.Columns[i].Visible = true;
            this.gwSearch.Columns[3].Visible = false;
            this.gwSearch.Columns[6].Visible = false;
            this.gwSearch.Columns[8].Visible = false;
            this.gwSearch.Columns[15].Visible = false;
            this.gwSearch.Columns[16].Visible = false;
            this.gwSearch.Columns[17].Visible = false;
            this.gwSearch.Columns[18].Visible = false;
            this.gwSearch.Columns[19].Visible = false;
            this.gwSearch.Columns[20].Visible = false;
            this.gwSearch.Columns[21].Visible = false;
            this.gwSearch.Columns[22].Visible = false;
            this.gwSearch.Columns[24].Visible = false;
            this.gwSearch.Columns[25].Visible = false;
            this.gwSearch.Columns[28].Visible = false;
            this.gwSearch.Columns[29].Visible = false;
            this.gwSearch.Columns[30].Visible = false;
            this.gwSearch.Columns[31].Visible = false;
            this.gwSearch.Columns[32].Visible = false;
            this.gwSearch.Columns[33].Visible = false;
            this.gwSearch.Columns[34].Visible = false;

            this.gwSearch.Width = 1000;

            mybind();
        }

        protected void gwSearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "outconfirm")
            {                
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate,jhnum,wl;
                int pid = int.Parse(gwSearch.DataKeys[gvr.RowIndex].Value.ToString());
                //BLL.emailinfo el = new BLL.emailinfo();
                //Model.emailinfo em = el.GetModelbypid(pid);
                //if (em == null)
                //{
                //    em.efax = "";
                //    em.ephone = "";
                //    em.linkname = "";                   
                //}
                jhnum = gvr.Cells[4].Text.Trim();
                wl = gvr.Cells[10].Text.Trim();
                id = gvr.Cells[5].Text.Trim();
                unit = ((Label)gvr.Cells[9].Controls[1]).Text;
                jdate = ((Label)gvr.Cells[14].Controls[1]).Text;
                proname = gvr.Cells[7].Text.Trim();
                //name = ((Label)gvr.Cells[25].Controls[1]).Text;
                //fax = ((Label)gvr.Cells[26].Controls[1]).Text;
                string file = ExWord.ExportToWordconfirm(unit, jdate,jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }
            if (e.CommandName == "outex")
            {
                //int index = int.Parse(e.CommandArgument.ToString());
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate, jhnum, wl;
                int pid = int.Parse(gwSearch.DataKeys[gvr.RowIndex].Value.ToString());                
                jhnum = gvr.Cells[4].Text.Trim();
                wl = gvr.Cells[10].Text.Trim();
                id = gvr.Cells[5].Text.Trim();
                unit = ((Label)gvr.Cells[9].Controls[1]).Text;
                jdate = ((Label)gvr.Cells[14].Controls[1]).Text;
                proname = gvr.Cells[7].Text.Trim();
                string file = ExWord.ExportToWordExped(unit, jdate, jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }

        }

        protected void gwSearch_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (true)//如果允许改变列宽
                {
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        e.Row.Cells[i].Attributes.Add("onmousemove", "SyDG_moveOnTd(this)");
                        e.Row.Cells[i].Attributes.Add("onmousedown", "SyDG_downOnTd(this)");
                        e.Row.Cells[i].Attributes.Add("onmouseup", "this.mouseDown=false");
                        e.Row.Cells[i].Attributes.Add("onmouseout", "this.mouseDown=false");
                    }
                }
            }

        }
        //更新选定项联系方式
        protected void btnUpdateAll_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
            {
                return;
            }

            string[] ids = idlist.Split(',');

            BLL.projectinfo pm = new BLL.projectinfo();
            BLL.supplierinfo sm = new BLL.supplierinfo();

            foreach (string id in ids)
            {
                Model.projectinfo p = pm.GetModel(int.Parse(id));
                string supplier = p.supplier.Trim();

                List<lcps.Model.supplierinfo> sl = sm.GetModelListByName(supplier.Trim());

                if (sl.Count == 1)
                {
                    Model.supplierinfo s = sl[0];

                    p.suppliername = s.suppliername;
                    p.supplierphone = s.supplierphone;

                    pm.UpdateGV(p);
                }
                else
                {

                }


            }

            mybind();
           
        }

        //选定项是否预警
        protected void btnIsWarn_Click(object sender, EventArgs e)
        {
            string idlist = "";
            if (this.rdoIsWarnY.Checked)//设定预警为是（0）
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;

                lcps.BLL.projectinfo p = new BLL.projectinfo();
                p.setIsWarn(idlist, "0");

            }
            if (this.rdoIsWarnN.Checked)//设定预警为否
            {
                idlist = GetSelIDlist();
                if (idlist.Trim().Length == 0)
                    return;

                lcps.BLL.projectinfo p = new BLL.projectinfo();
                p.setIsWarn(idlist, "1");
            }
            mybind();

        }

        protected void btninstore_Click(object sender, EventArgs e)
        {
            lcps.BLL.projectinfo bp = new BLL.projectinfo();
            string sql = hfsql.Value;
            DataSet ds = null;
            if (sql == "")
            {
                if (ddltables.SelectedValue != "所有")
                    ds = bp.GetList(" tableid=" + ddltables.SelectedValue);
                else
                    ds = bp.GetList("");
            }
            else
            {
                if (ddltables.SelectedValue != "所有")
                    ds = bp.GetList(sql + " and tableid=" + ddltables.SelectedValue);
                else
                    ds = bp.GetList(sql);
            }
            string proid,usework, projectname, projecttype,supplier,material;
            float nums;
            string unit, suppliername, supplierphone;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int id=int.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                //ID,proid,bidid,purchaseid,usework,projectname,projecttype,supplier,materiel,unit,nums,contractdate,deliverydate,confirmdate,arrivaldate,workdate,drawingplandate,drawingstate,drawingsubmitdate,drawingconfirmdate,designstate,majormaterielpurchase,deviceproductionstate,mothstate,nextmoth,planarrivedate,coordinate,remark,realdate,receivedname,receivedphone,suppliername,supplierphone,checkstate,transactdate,agent,tableid,dealStorageDate,isWarn;
                proid = ds.Tables[0].Rows[i]["proid"].ToString();
                usework = ds.Tables[0].Rows[i]["usework"].ToString();
                projectname = ds.Tables[0].Rows[i]["projectname"].ToString();
                projecttype = ds.Tables[0].Rows[i]["projecttype"].ToString();
                supplier = ds.Tables[0].Rows[i]["supplier"].ToString();
                material = ds.Tables[0].Rows[i]["materiel"].ToString();
                nums = float.Parse( ds.Tables[0].Rows[i]["nums"].ToString());
                unit = ds.Tables[0].Rows[i]["unit"].ToString();
                suppliername = ds.Tables[0].Rows[i]["suppliername"].ToString();
                supplierphone = ds.Tables[0].Rows[i]["supplierphone"].ToString();
                if (bp.Isup(id))
                {
                    Add(proid, usework, projectname, projecttype, supplier, material, nums, unit, suppliername, supplierphone);
                    bp.setIsup(id);
                }

            }
            Maticsoft.Common.MessageBox.Show(this, "导入成功！");
        }
        public int Add(string proid,string usework,string projectname,string projecttype,string supplier,string material,float nums,string unit,string suppliername,string supplierphone)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into materialinfo(");
            strSql.Append("proid,usework,projectname,projecttype,supplier,material,nums,unit,suppliername,supplierphone,mstatus,uid,mtype)");
            strSql.Append(" values (");
            strSql.Append("@proid,@usework,@projectname,@projecttype,@supplier,@material,@nums,@unit,@suppliername,@supplierphone,@mstatus,@uid,@mtype)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@proid", SqlDbType.NVarChar,50),
					new SqlParameter("@usework", SqlDbType.NVarChar,50),
					new SqlParameter("@projectname", SqlDbType.NVarChar,50),
					new SqlParameter("@projecttype", SqlDbType.NVarChar,50),
					new SqlParameter("@supplier", SqlDbType.NVarChar,50),
					new SqlParameter("@material", SqlDbType.NVarChar,250),
					new SqlParameter("@nums", SqlDbType.Float,8),
					new SqlParameter("@unit", SqlDbType.NChar,10),
					new SqlParameter("@suppliername", SqlDbType.NVarChar,50),
					new SqlParameter("@supplierphone", SqlDbType.NVarChar,150),
					new SqlParameter("@mstatus", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@mtype", SqlDbType.Int,4)};
            parameters[0].Value =proid;
            parameters[1].Value = usework;
            parameters[2].Value = projectname;
            parameters[3].Value = projecttype;
            parameters[4].Value = supplier;
            parameters[5].Value = material;
            parameters[6].Value = nums;
            parameters[7].Value = unit;
            parameters[8].Value = suppliername;
            parameters[9].Value = supplierphone;
            parameters[10].Value = 0;
            parameters[11].Value = 0;
            parameters[12].Value = 0;

            object obj = Maticsoft.DBUtility.DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}